/*
 *  edb_net_utils.c
 *  edb
 *
 *  Created by Grant Jones on 3/22/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "edb_net_utils.h"

#include <openssl/bio.h>
#include <openssl/evp.h>

#include <ctype.h>

static char *uri_encode[256] = {
"%00",
"%01",
"%02",
"%03",
"%04",
"%05",
"%06",
"%07",
"%08",
"%09",
"%0a",
"%0b",
"%0c",
"%0d",
"%0e",
"%0f",
"%10",
"%11",
"%12",
"%13",
"%14",
"%15",
"%16",
"%17",
"%18",
"%19",
"%1a",
"%1b",
"%1c",
"%1d",
"%1e",
"%1f",
"+\0\0",
"%21",
"%22",
"%23",
"%24",
"%25",
"%26",
"%27",
"%28",
"%29",
"*\0\0",
"%2b",
"%2c",
"-\0\0",
".\0\0",
"%2f",
"0\0\0",
"1\0\0",
"2\0\0",
"3\0\0",
"4\0\0",
"5\0\0",
"6\0\0",
"7\0\0",
"8\0\0",
"9\0\0",
"%3a",
"%3b",
"%3c",
"%3d",
"%3e",
"%3f",
"%40",
"A\0\0",
"B\0\0",
"C\0\0",
"D\0\0",
"E\0\0",
"F\0\0",
"G\0\0",
"H\0\0",
"I\0\0",
"J\0\0",
"K\0\0",
"L\0\0",
"M\0\0",
"N\0\0",
"O\0\0",
"P\0\0",
"Q\0\0",
"R\0\0",
"S\0\0",
"T\0\0",
"U\0\0",
"V\0\0",
"W\0\0",
"X\0\0",
"Y\0\0",
"Z\0\0",
"%5b",
"%5c",
"%5d",
"%5e",
"_\0\0",
"%60",
"a\0\0",
"b\0\0",
"c\0\0",
"d\0\0",
"e\0\0",
"f\0\0",
"g\0\0",
"h\0\0",
"i\0\0",
"j\0\0",
"k\0\0",
"l\0\0",
"m\0\0",
"n\0\0",
"o\0\0",
"p\0\0",
"q\0\0",
"r\0\0",
"s\0\0",
"t\0\0",
"u\0\0",
"v\0\0",
"w\0\0",
"x\0\0",
"y\0\0",
"z\0\0",
"%7b",
"%7c",
"%7d",
"%7e",
"%7f",
"%80",
"%81",
"%82",
"%83",
"%84",
"%85",
"%86",
"%87",
"%88",
"%89",
"%8a",
"%8b",
"%8c",
"%8d",
"%8e",
"%8f",
"%90",
"%91",
"%92",
"%93",
"%94",
"%95",
"%96",
"%97",
"%98",
"%99",
"%9a",
"%9b",
"%9c",
"%9d",
"%9e",
"%9f",
"%a0",
"%a1",
"%a2",
"%a3",
"%a4",
"%a5",
"%a6",
"%a7",
"%a8",
"%a9",
"%aa",
"%ab",
"%ac",
"%ad",
"%ae",
"%af",
"%b0",
"%b1",
"%b2",
"%b3",
"%b4",
"%b5",
"%b6",
"%b7",
"%b8",
"%b9",
"%ba",
"%bb",
"%bc",
"%bd",
"%be",
"%bf",
"%c0",
"%c1",
"%c2",
"%c3",
"%c4",
"%c5",
"%c6",
"%c7",
"%c8",
"%c9",
"%ca",
"%cb",
"%cc",
"%cd",
"%ce",
"%cf",
"%d0",
"%d1",
"%d2",
"%d3",
"%d4",
"%d5",
"%d6",
"%d7",
"%d8",
"%d9",
"%da",
"%db",
"%dc",
"%dd",
"%de",
"%df",
"%e0",
"%e1",
"%e2",
"%e3",
"%e4",
"%e5",
"%e6",
"%e7",
"%e8",
"%e9",
"%ea",
"%eb",
"%ec",
"%ed",
"%ee",
"%ef",
"%f0",
"%f1",
"%f2",
"%f3",
"%f4",
"%f5",
"%f6",
"%f7",
"%f8",
"%f9",
"%fa",
"%fb",
"%fc",
"%fd",
"%fe",
"%ff"
};
static unsigned char uri_encode_sz[256] = {
3, /* 0x0 */
3, /* 0x1 */
3, /* 0x2 */
3, /* 0x3 */
3, /* 0x4 */
3, /* 0x5 */
3, /* 0x6 */
3, /* 0x7 */
3, /* 0x8 */
3, /* 0x9 */
3, /* 0xa */
3, /* 0xb */
3, /* 0xc */
3, /* 0xd */
3, /* 0xe */
3, /* 0xf */
3, /* 0x10 */
3, /* 0x11 */
3, /* 0x12 */
3, /* 0x13 */
3, /* 0x14 */
3, /* 0x15 */
3, /* 0x16 */
3, /* 0x17 */
3, /* 0x18 */
3, /* 0x19 */
3, /* 0x1a */
3, /* 0x1b */
3, /* 0x1c */
3, /* 0x1d */
3, /* 0x1e */
3, /* 0x1f */
1, // space
3, /* 0x21 */
3, /* 0x22 */
3, /* 0x23 */
3, /* 0x24 */
3, /* 0x25 */
3, /* 0x26 */
3, /* 0x27 */
3, /* 0x28 */
3, /* 0x29 */
1, /* * */
3, /* 0x2b */
3, /* 0x2c */
1, /* - */
1, /* . */
3, /* 0x2f */
1, /* 0 */
1, /* 1 */
1, /* 2 */
1, /* 3 */
1, /* 4 */
1, /* 5 */
1, /* 6 */
1, /* 7 */
1, /* 8 */
1, /* 9 */
3, /* 0x3a */
3, /* 0x3b */
3, /* 0x3c */
3, /* 0x3d */
3, /* 0x3e */
3, /* 0x3f */
3, /* 0x40 */
1, /* A */
1, /* B */
1, /* C */
1, /* D */
1, /* E */
1, /* F */
1, /* G */
1, /* H */
1, /* I */
1, /* J */
1, /* K */
1, /* L */
1, /* M */
1, /* N */
1, /* O */
1, /* P */
1, /* Q */
1, /* R */
1, /* S */
1, /* T */
1, /* U */
1, /* V */
1, /* W */
1, /* X */
1, /* Y */
1, /* Z */
3, /* 0x5b */
3, /* 0x5c */
3, /* 0x5d */
3, /* 0x5e */
1, /* _ */
3, /* 0x60 */
1, /* a */
1, /* b */
1, /* c */
1, /* d */
1, /* e */
1, /* f */
1, /* g */
1, /* h */
1, /* i */
1, /* j */
1, /* k */
1, /* l */
1, /* m */
1, /* n */
1, /* o */
1, /* p */
1, /* q */
1, /* r */
1, /* s */
1, /* t */
1, /* u */
1, /* v */
1, /* w */
1, /* x */
1, /* y */
1, /* z */
3, /* 0x7b */
3, /* 0x7c */
3, /* 0x7d */
3, /* 0x7e */
3, /* 0x7f */
3, /* 0x80 */
3, /* 0x81 */
3, /* 0x82 */
3, /* 0x83 */
3, /* 0x84 */
3, /* 0x85 */
3, /* 0x86 */
3, /* 0x87 */
3, /* 0x88 */
3, /* 0x89 */
3, /* 0x8a */
3, /* 0x8b */
3, /* 0x8c */
3, /* 0x8d */
3, /* 0x8e */
3, /* 0x8f */
3, /* 0x90 */
3, /* 0x91 */
3, /* 0x92 */
3, /* 0x93 */
3, /* 0x94 */
3, /* 0x95 */
3, /* 0x96 */
3, /* 0x97 */
3, /* 0x98 */
3, /* 0x99 */
3, /* 0x9a */
3, /* 0x9b */
3, /* 0x9c */
3, /* 0x9d */
3, /* 0x9e */
3, /* 0x9f */
3, /* 0xa0 */
3, /* 0xa1 */
3, /* 0xa2 */
3, /* 0xa3 */
3, /* 0xa4 */
3, /* 0xa5 */
3, /* 0xa6 */
3, /* 0xa7 */
3, /* 0xa8 */
3, /* 0xa9 */
3, /* 0xaa */
3, /* 0xab */
3, /* 0xac */
3, /* 0xad */
3, /* 0xae */
3, /* 0xaf */
3, /* 0xb0 */
3, /* 0xb1 */
3, /* 0xb2 */
3, /* 0xb3 */
3, /* 0xb4 */
3, /* 0xb5 */
3, /* 0xb6 */
3, /* 0xb7 */
3, /* 0xb8 */
3, /* 0xb9 */
3, /* 0xba */
3, /* 0xbb */
3, /* 0xbc */
3, /* 0xbd */
3, /* 0xbe */
3, /* 0xbf */
3, /* 0xc0 */
3, /* 0xc1 */
3, /* 0xc2 */
3, /* 0xc3 */
3, /* 0xc4 */
3, /* 0xc5 */
3, /* 0xc6 */
3, /* 0xc7 */
3, /* 0xc8 */
3, /* 0xc9 */
3, /* 0xca */
3, /* 0xcb */
3, /* 0xcc */
3, /* 0xcd */
3, /* 0xce */
3, /* 0xcf */
3, /* 0xd0 */
3, /* 0xd1 */
3, /* 0xd2 */
3, /* 0xd3 */
3, /* 0xd4 */
3, /* 0xd5 */
3, /* 0xd6 */
3, /* 0xd7 */
3, /* 0xd8 */
3, /* 0xd9 */
3, /* 0xda */
3, /* 0xdb */
3, /* 0xdc */
3, /* 0xdd */
3, /* 0xde */
3, /* 0xdf */
3, /* 0xe0 */
3, /* 0xe1 */
3, /* 0xe2 */
3, /* 0xe3 */
3, /* 0xe4 */
3, /* 0xe5 */
3, /* 0xe6 */
3, /* 0xe7 */
3, /* 0xe8 */
3, /* 0xe9 */
3, /* 0xea */
3, /* 0xeb */
3, /* 0xec */
3, /* 0xed */
3, /* 0xee */
3, /* 0xef */
3, /* 0xf0 */
3, /* 0xf1 */
3, /* 0xf2 */
3, /* 0xf3 */
3, /* 0xf4 */
3, /* 0xf5 */
3, /* 0xf6 */
3, /* 0xf7 */
3, /* 0xf8 */
3, /* 0xf9 */
3, /* 0xfa */
3, /* 0xfb */
3, /* 0xfc */
3, /* 0xfd */
3, /* 0xfe */
3 /* 0xff */
};



cmapStruct *edb_net_base64string( mempool_t *pool, const char *z, size_t n )
{
	BIO *bio, *b64, *mem;
	char inbuf[513];
	int inlen;
	
	cmapStruct *result_map = cmap_new(pool);

	b64 = BIO_new(BIO_f_base64());
	BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL );		/* we don't want a newline at the end */
	mem = BIO_new(BIO_s_mem());
	bio = BIO_push(b64, mem );							/* base-64 filter -> memory sink buffer */
	BIO_write(bio, z, n );	
	(void)BIO_flush(bio);

	while((inlen = BIO_read(mem, inbuf, 512)) > 0)
		cmap_str_append_bytes(result_map, inbuf, inlen);
	
	BIO_free_all(bio);

	return result_map;
}


char *edb_net_URIEncode( mempool_t *pool, const unsigned char *z, size_t n )
{
	/* in my defense, I completely forgot that libevent provides most of this
	** type of functionality.. still, fun to reinvent the wheel ^^
	*/
	char *enc_str = "";
	char *enc_str_tmp;
	size_t enc_len = 0;
	size_t i;
	for( i = 0; i < n; i ++ )
		enc_len += uri_encode_sz[z[i]];
	if(enc_len <= 0)
		return enc_str;
	enc_str_tmp = enc_str = mempool_malloc_named(pool, enc_len+3+1, __FUNCTION__);
	if(!enc_str)
		return NULL;
	for( i = 0; i < n; i ++ )
	{
		char *char_enc = uri_encode[z[i]];
		*(enc_str_tmp) = char_enc[0];
		*(enc_str_tmp+1) = char_enc[1];
		*(enc_str_tmp+2) = char_enc[2];
		enc_str_tmp += uri_encode_sz[z[i]];
	}
	*enc_str_tmp = '\0';

	return enc_str;
}
/*
** libevent's evhttp_decode_uri requires there be a '?' character before it starts
** decoding a URI. evhttp_parse_query calls evhttp_decode_uri for each value, but
** I'm not really sure why since there will be no '?' character.
** 
** this version of URIDecode does not require a '?' character, it is assumed that we
** are positive this is a query. 
*/
char *edb_net_URIDecode( mempool_t *pool, const unsigned char *z, size_t n )
{
	char *dec_str;
	char *dec_str_tmp;
	char c;
	size_t i;
	dec_str_tmp = dec_str = mempool_malloc_named(pool, n+1, __FUNCTION__);
	if(!dec_str)
		return NULL;
	for( i = 0; i < n; i ++ )
	{
		c = z[i];
		switch(c)
		{
		case '+':
			c = ' ';
			break;
		case '%':
			if( isxdigit((unsigned char)z[i+1]) && isxdigit((unsigned char)z[i+2]) )
			{
				char tmp[] = { z[i+1], z[i+2], '\0' };
				c = (char)strtol(tmp, NULL, 16);
				i += 2;			
			}
			else
			{
				c = '%';	/* this isn't a valid decoding case */
			}
			break;	
		default:
			break;
		}
		*dec_str_tmp = c;
		dec_str_tmp ++;
	}
	
	return dec_str;	
}

